package com.mayisoftware.mayioa.business.pool.impl.controller;

import com.mayisoftware.mayioa.business.plan.api.domain.PlanConfig;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanConfigService;
import com.mayisoftware.mayioa.business.pool.api.domain.PoolConstants;
import com.mayisoftware.mayioa.business.pool.api.domain.PoolFigure;
import com.mayisoftware.mayioa.business.pool.api.domain.PoolMgrInfo;
import com.mayisoftware.mayioa.business.pool.api.service.IPoolFigureService;
import com.mayisoftware.mayioa.business.pool.api.service.IPoolManualService;
import com.mayisoftware.mayioa.business.score.api.service.IScoreInfoService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.constant.Constants;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.common.impl.support.Convert;
import com.mayisoftware.mayioa.common.impl.utils.ExcelUtil;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import com.mayisoftware.mayioa.platform.api.service.ISysUserService;
import com.mayisoftware.mayioa.platform.impl.controller.system.SysProfileController;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Controller
@RequestMapping("/pool/figure")
public class PoolFigureController extends BaseController{

    private static final Logger log = LoggerFactory.getLogger(PoolFigureController.class);

    /**
     * 词典服务
     */
    @Autowired
    ISysDictDataService sysDictDataService;

    /**
     * 用户服务
     */
    @Autowired
    ISysUserService sysUserService;
    /**
     * 骨干画像查询服务
     */
    @Autowired
    IPoolFigureService poolFigureService;

    /**
     * 打分管理注入
     */
    @Autowired
    IScoreInfoService scoreInfoService;

    /**
     * 计划信息查询
     */
    @Autowired
    IPlanConfigService planConfigService;

    /**
     * 前缀
     */
    private String prefix = "pool/figure";

    @RequiresPermissions("pool:figure:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/figure";
    }

    @RequiresPermissions("pool:figure:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PoolFigure record)
    {
        startPage();
        List<PoolFigure> poolFigures = poolFigureService.queryBackboneList(record);

        formatPoolFigures(poolFigures);


        return getDataTable(poolFigures);

    }

    private void formatPoolFigures(List<PoolFigure> poolFigures) {
        if (CollectionUtils.isEmpty(poolFigures)){
            return;
        }

        for (PoolFigure poolFigure:poolFigures){
            if(null != poolFigure.getJoinType()){
                poolFigure.setJoinTypeStr(sysDictDataService.selectDictLabel("pool_mgr_create_type",poolFigure.getJoinType().toString()));
            }


            if (null !=poolFigure.getJoinTime() ){
                poolFigure.setJoinTimeStr(Constants.YMD_HMS_FMT.format(poolFigure.getJoinTime()));
            }

            if (null != poolFigure.getSex()){
                poolFigure.setSex(sysDictDataService.selectDictLabel("sys_user_sex",poolFigure.getSex()));
            }

            if (null != poolFigure.getHwLevel()){
                poolFigure.setSex(sysDictDataService.selectDictLabel("sys_hw_level",poolFigure.getHwLevel()));
            }

            if (null != poolFigure.getCoopModel()){
                poolFigure.setSex(sysDictDataService.selectDictLabel("sys_coop_model",poolFigure.getCoopModel()));
            }

            if (null != poolFigure.getJoinCpyTime()){
                poolFigure.setJoinCpyTimeStr(Constants.YMD_FMT.format(poolFigure.getJoinCpyTime()));
            }

            if (null != poolFigure.getWorkTime()){
                poolFigure.setWorkTimeStr(Constants.YMD_FMT.format(poolFigure.getWorkTime()));
            }

            if (null != poolFigure.getIs211()){
                poolFigure.setIs211(sysDictDataService.selectDictLabel("sys_school_211",poolFigure.getIs211()));
            }

            if (null != poolFigure.getRegion()){
                poolFigure.setRegion(sysDictDataService.selectDictLabel("sys_user_region",poolFigure.getRegion()));
            }

            if (null != poolFigure.getProjectRole()){
                poolFigure.setProjectRole(sysDictDataService.selectDictLabel("sys_user_project_role",poolFigure.getProjectRole()));
            }


            Long directUserId = poolFigure.getDirectSuperiorUserId();

            if (directUserId != null){
                SysUser directUser = sysUserService.selectUserById(directUserId);

                if (directUser != null){
                    poolFigure.setDirectSuperiorUserName(directUser.getUserName());
                }
            }
        }
    }

//    private  List<PoolFigure>  queryAllBackbones(List<PoolMgrInfo> poolMgrInfos){
//        //查询骨干信息
//        List<PoolFigure> poolFigures =new ArrayList<>();
//        if (CollectionUtils.isEmpty(poolMgrInfos)){
//            return poolFigures;
//        }
//
//        List<SysUser> users = sysUserService.selectUserList(new SysUser());
//        Map<Long,SysUser> id_user=new HashMap<>();
//        for (SysUser user:users){
//            id_user.put(user.getUserId(),user);
//        }
//
//        for (PoolMgrInfo mgrInfo:poolMgrInfos){
//            Integer userId = mgrInfo.getPoolUserId();
//            if (null == userId){
//                continue;
//            }
//
//            SysUser sysUser= id_user.get(userId.longValue());
//
//            if (null == sysUser){
//                log.warn("不存在该用户的ID:"+userId);
//                continue;
//            }
//            PoolFigure poolFigure = new PoolFigure();
//
//            poolFigure.setUserId(userId);
//            poolFigure.setUserName(sysUser.getUserName());
//            Integer planId = mgrInfo.getPoolPlanId();
//
//            //没有计划 可能是手动加入
//            if (null != planId){
//                PlanConfig planConfig = planConfigService.selectPlanConfigById(planId.longValue());
//
//                if (planConfig == null){
//                    log.warn("计划不存在"+planId);
//                    continue;
//                }
//                poolFigure.setPlanName(planConfig.getPlanCfgName());
//                poolFigure.setPlanId(planConfig.getPlanCfgId());
//                poolFigure.setPlanRank(mgrInfo.getPoolPlanRank());
//                poolFigure.setPlanType(planConfig.getPlanCfgType());
//                poolFigure.setPlanLastn(mgrInfo.getPoolPlanLastn());
//            }
//
//            poolFigure.setJoinDesc(mgrInfo.getPoolInDesc());
//            poolFigure.setTotalScore(mgrInfo.getPoolTotalScore());
//            poolFigure.setJoinType(mgrInfo.getPoolCreateType());
//
//            if(null != mgrInfo.getPoolCreateType()){
//                poolFigure.setJoinTypeStr(sysDictDataService.selectDictLabel("pool_mgr_create_type",mgrInfo.getPoolCreateType().toString()));
//            }
//
//            if (null != sysUser.getDept()){
//                if (sysUser.getDept().getDeptId() != null){
//                    poolFigure.setDeptId( sysUser.getDept().getDeptId().toString());
//                }
//                poolFigure.setDeptName(sysUser.getDept().getDeptName());
//            }
//
//            if (null !=mgrInfo.getPoolInTime() ){
//                poolFigure.setJoinTime(mgrInfo.getPoolInTime());
//                poolFigure.setJoinTimeStr(Constants.YMD_HMS_FMT.format(mgrInfo.getPoolInTime()));
//            }
//
//            poolFigure.setEmail(sysUser.getEmail());
//            poolFigure.setPhonenumber(sysUser.getPhonenumber());
//            poolFigure.setSex(sysDictDataService.selectDictLabel("sys_user_sex",sysUser.getSex()));
//            poolFigure.setHwLevel(sysDictDataService.selectDictLabel("sys_hw_level",sysUser.getHwLevel()));
//            poolFigure.setCoopModel(sysDictDataService.selectDictLabel("sys_coop_model",sysUser.getCoopModel()));
//
//            if (null != sysUser.getJoinTime()){
//                poolFigure.setJoinCpyTime(sysUser.getJoinTime());
//                poolFigure.setJoinCpyTimeStr(Constants.YMD_FMT.format(sysUser.getJoinTime()));
//            }
//
//            if (null != sysUser.getWorkTime()){
//                poolFigure.setWorkTime(sysUser.getWorkTime());
//                poolFigure.setWorkTimeStr(Constants.YMD_FMT.format(sysUser.getWorkTime()));
//            }
//
//            poolFigure.setSchool(sysUser.getSchool());
//            poolFigure.setEducation(sysUser.getEducation());
//            poolFigure.setIs211(sysDictDataService.selectDictLabel("sys_school_211",sysUser.getIs211()));
//            poolFigure.setRegion(sysDictDataService.selectDictLabel("sys_user_region",sysUser.getRegion()));
//            poolFigure.setProjectRole(sysDictDataService.selectDictLabel("sys_user_project_role",sysUser.getProjectRole()));
//
//            Long directUserId = sysUser.getDirectSuperiorUserId();
//
//            if (directUserId != null){
//                poolFigure.setDirectSuperiorUserId(sysUser.getDirectSuperiorUserId());
//                SysUser directUser = sysUserService.selectUserById(directUserId);
//
//                if (directUser != null){
//                    poolFigure.setDirectSuperiorUserName(directUser.getUserName());
//                }
//            }
//
//            poolFigure.setRsv1(sysUser.getRsv1());
//            poolFigure.setRsv2(sysUser.getRsv2());
//            poolFigure.setRsv3(sysUser.getRsv3());
//            poolFigure.setRsv4(sysUser.getRsv4());
//            poolFigure.setRsv5(sysUser.getRsv5());
//            poolFigures.add(poolFigure);
//        }
//
//        return poolFigures;
//    }

    @Log(title = "导出骨干信息", businessType = BusinessType.EXPORT)
    @RequiresPermissions("pool:figure:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PoolFigure info)
    {
        List<PoolFigure> poolFigures = poolFigureService.queryBackboneList(info);
        formatPoolFigures(poolFigures);
        ExcelUtil<PoolFigure> util = new ExcelUtil<PoolFigure>(PoolFigure.class);
        return util.exportExcel(poolFigures, "骨干信息");
    }
}

